// Additional options and functions from Analogus
// Initial version: 2012.03.06
// Author: ANALOGUS
//
// Description: 
// Various options, actions & functions; big sandbox to play with new functions
// 2012-04-14: changed option group
// 2012-04-27: code shortening 
// 2012-06-10: Mark Nord - UTF-8 encoding, var-defs, now working in BETA-folder

tmp = function() {
	
	
	//Shortcuts:
		NAME = "Analogus_Options";
		LA = Core.lang.getLocalizer("ANALOGUS");
		log = Core.log.getLogger(NAME);
		MSG = Core.ui.showMsg;
		model = kbook.model;
		exec = Core.shell.exec;
		PATH_SIZEOVERLAY = model.container.sandbox.SIZE_OVERLAY_GROUP.sandbox.VIEW.sandbox.SIZE_OVERLAY.sandbox;

	//AutoRestart-Function:
	AutoRestart = function () 
		{
			switch (Core.config.model)	//Model-Prüfung
				{
				case 'xxx350':	//temp. deactivated 
					//less dirty restart-mode: tries to format external MC-card and restarts reader normally
					//Problem: resart OK but it seems that it does not restart from the bottom. Installer (e.g. PRS+ installer) do not work.
					MSG(LA("MSG_RESTART_AUTO")); 
					ebook.setExitCode(5);
					model.doDeviceShutdown();	
					break;
				default:
					//brute restart-mode
					model.commitCache();	//tries to save unsaved parts of the system cache; problem: does not work in all cases
					MSG(LA("MSG_RESTART_AUTO"));		
					exec('reboot');		
				}
			};

	
/*
	//not functional. Code functional but works only when activated in an open book. 
	SizeSymbolFunction = function (size)	
		{
		switch (size) 
			{
			case 'ON':
				model.container.sandbox.STATUS_GROUP.sandbox.STATUS_GROUP_SUB.sandbox.STATUS_GROUP.sandbox.showBookSizeIndicator(true);
				break;

			case 'OFF':
				model.container.sandbox.STATUS_GROUP.sandbox.STATUS_GROUP_SUB.sandbox.STATUS_GROUP.sandbox.showBookSizeIndicator(false); 
				break;
			}
		}	
*/
	
	//Sys-Log & Beta
	BetalogFunction = function (beta) {
		switch (beta) 
		{
			case 'BetaOn':
				exec('cp -f /Data/database/system/PRSPlus/UserConfig/userB.config /Data/Beta/user.config');
				MSG(LA("MSG_BETA_ACTIVATE")); 
				break ;
			case 'LogOn':
				exec('mkdir -p /Data/Beta; cp -f /Data/database/system/PRSPlus/UserConfig/userLog.config /Data/Beta/user.config');
				MSG(LA("MSG_LOG_ACTIVATE")); 
				break ;
			case 'LogOff':
				exec('rm -f /Data/Beta/user.config; rm -f /Data/Books/ErrorLog.txt'); 
				MSG(LA("MSG_LOG_DEACTIVATE")); 
				break ;
		}
		MSG(LA("MSG_RESTART")); 
	};
	

//---------taking commands from / functions for Font_Size_Overlay-buttons:---------------------

	
	//variable button in Font_Size_Overlay;
		PATH_SIZEOVERLAY.doVariable = function () {
			var option = Analogus_Options.options.VarButtonSizeOverlay;
			switch (option)
			{
				case 'value1':
					kbook.model.currentNode.gotoNode(Core.ui.nodes.StandardActions, model);	//opens SwissKnife
					break;
				case 'value2':
					var toc = kbook.bookOptionRoot.contents;	//opens TOC
					if (toc) {
						model.gotoBookOptionList(toc);
					} else {
						model.doBlink();}
					break;
				case 'value3':
						pageOptionToolbarOverlayModel.onOptionToolbar();	//opens notes
					break;
				case 'value4':
					model.doRoot();		//goto root
					break;
				case 'value5':
					
					break;					
				case 'value6':
					
					break;	
				case 'value7':
					
					break;	
			}
		};
	
	/*not OK:

			model.currentNode.gotoNode(Core.ui.nodes.StandardActions, model);
			doBubble("doRotate", 1);
			kbook.model.doBubble("doRotate", 1);
			doHistory(1);
			model.doHistory(1);
			kbook.notepadsText ? kbook.notepadsText : kbook.root.getNotepadsTextNode();	
			kbook.model.currentNode.gotoNode(node, model);
			ReformatBook();	
			ResetCSS();
			model.ResetCSS(); 
			this.ResetCSS();
			ResetCSS;
			this.ResetCSS;
			
			
		OK:
	
			model.doDeviceShutdown(); //shutdown
			model.doRoot();	
			pageOptionToolbarOverlayModel.onOptionToolbar();	

			
*/	
	

//--------------------------------------------------------------------------------------------------------
	Analogus_Options = 
	{
		name: NAME,
		settingsGroup:"additional",
		//title: LA("GROUP_ADD_OPTIONS"),	//erzeugt Optionsgruppe in PRS-options?
		icon: "LAUNCHER", 
		//------------------------------------------------------------------------------------------------
		//Creates options and options-groups:
		optionDefs:
		[
			{
				name: 'Betalog',	
				title: LA('OPTION_BETA_LOG'),
				helpText: LA('HELP_BETA_LOG'),
				icon: 'INTERNAL_MEM',
				defaultValue: 'DEACTIVATE_LOG',
				values: ['BetaOn', 'LogOn', 'LogOff'],
				valueTitles: {
					'BetaOn': LA('ACTIVATE_BETA'),	
					'LogOn': LA('ACTIVATE_LOG'),
					'LogOff': LA('DEACTIVATE_LOG')}
			},

			{
				name: 'VarButtonSizeOverlay',	
				title: LA('OPT_BUTT_SIZE_OVERLAY'),
				helpText: LA('HELP_BUTT_SIZE_OVERLAY'),
				icon: 'CASE',
				defaultValue: 'value4',
				values: ['value1', 'value2', 'value3', 'value4'],
				valueTitles: 
					{
					'value1': LA('OPT_BUTT_1'),	
					'value2': LA('OPT_BUTT_2'),
					'value3': LA('OPT_BUTT_3'),
					'value4': LA('OPT_BUTT_4')
					}
			}
		],

		onInit: function () //calls options-values and activates functions with values on restart:
			{
				BatteryFunction(Analogus_Options.options.Battery);	//function(value of function from saved options);
				//SizeSymbolFunction(Analogus_Options.options.SizeSymbol);
			},
			
		actions: 
			[
			//Developer-Tools:
				{
					name: "showValue1",
					title: LA("Reload_Book"),
					group: "PRS_DEVELOPER_TOOLS",
					icon: "BACK", 
					action: function () 
					{
						BookReload();	//OK;
					} 
				}, 
			
				{
					name: "showValue2",
					title: LA("showValue2"),
					group: "PRS_DEVELOPER_TOOLS",
					icon: "INFO",
					action: function () 
					{
						//kbook.kbookPage.getMarginRemove();  
						//this.kbook.kbookPage.getMarginRemove();  
						//this.resizePageMarginRemove();
						//this.setVariable('PAGE_STYLE_NO', '3');
						//setVariable('PAGE_STYLE_NO', '3');
						//this.pageSelectStyleOverlayModel.doMarginRemove(); 
						//MSG(this.kbook.kbookPage.getMarginRemove()); 
						
						MSG(marginWidth()); 
						
					} 
				},
				
				{
					name: "showValue3",
					title: LA("FillInSerifTags"),
					group: "PRS_DEVELOPER_TOOLS",
					icon: "INFO",
					action: function () 
					{
						FillInSerifTags(); 
					} 
				},				
				
				{
					name: "showValue4",
					title: LA("showValue4"),
					group: "PRS_DEVELOPER_TOOLS",
					icon: "INFO",
					action: function () 
					{	
						MSG(marginWidth); 
					} 
				},

				{
					name: "showValue5",
					title: LA("showValue5"),
					group: "PRS_DEVELOPER_TOOLS",
					icon: "INFO", 
					action: function () 
					{
						MSG(this.marginWidth); 
						
					} 
				},		
		
				//Auto-Reboot-Action:
				{
					name: "RestartReader",
					title: LA("RESTART_READER"),
					group: "PRS_DEVELOPER_TOOLS",
					icon: "BACK",
					action: function () 
						{
						AutoRestart();
						}
				}
			],			
		
		//----------------------------------------------------------------------------------------------------------
		//Called from Core_Settings.js after changing options:
		
		onSettingsChanged: function (propertyName, oldValue, newValue, object) {
			switch (propertyName) 
			{
				case 'SizeSymbol':
					SizeSymbolFunction (newValue) ;
					break; 
				case 'VarButtonSizeOverlay':
					break; 
			}
		}
	};

	Core.addAddon(Analogus_Options);
};


try 
{
	tmp();
} catch (e) 
{
	// Core's log
	log.error("in Analogus_Options.js", e);
}
